package com.study.testSpringBoot.mina;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.charset.Charset;

import org.apache.mina.core.service.IoAcceptor;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.codec.textline.LineDelimiter;
import org.apache.mina.filter.codec.textline.TextLineCodecFactory;
import org.apache.mina.filter.logging.LogLevel;
import org.apache.mina.filter.logging.LoggingFilter;
import org.apache.mina.transport.socket.nio.NioSocketAcceptor;

public class MinaServer {

    public static void main( String[] args ) throws IOException
    {
    	//1、创建IoService，拥有监听是否有客户端链接，TCP
    	IoAcceptor acceptor=new NioSocketAcceptor();  
//    	IoAcceptor acceptor=new NioDatagramAcceptor();//UDP
    	//设置缓冲区大小
    	acceptor.getSessionConfig().setReadBufferSize(2048);  
    	//设置空闲状态时间，10秒没操作就进入空闲状态
    	acceptor.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE,10);  
    	
    	//创建日志过滤器
    	LoggingFilter log = new LoggingFilter();
    	log.setSessionOpenedLogLevel(LogLevel.INFO);
    	
    	//2、实现过滤器
    	acceptor.getFilterChain().addLast("logger", log);
    	acceptor.getFilterChain().addLast("test",  
			new ProtocolCodecFilter(
				new TextLineCodecFactory(Charset.forName("UTF-8"), LineDelimiter.WINDOWS.getValue(), LineDelimiter.WINDOWS.getValue())
			)  
		);
//    	acceptor.getFilterChain().addLast("myFilter", new MyFilter());
    	//3、实现IoHandler，并注册到IoService
    	acceptor.setHandler(new MinaServerHandler());  
    	//绑定端口，绑定之前必须设置handler实现类
    	acceptor.bind(new InetSocketAddress(9123)); 
    	
    }
}